// pages/fillInformations/fillInformations.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wanShow: false,
    userInfo: null,
    inforList:[
      {title:'年龄',text:'请输入您的年龄',val:''},
      {title:'身高',text:'请输入您的身高(cm)',val:''},
      {title:'体重',text:'请输入您的体重(斤)',val:''},
    ],
    genderList: [
      {value: 'man', name: '男',checked:true},
      {value: 'women', name: '女',checked:false},
    ],
    pupStatus:false,//性别选择弹框状态
    nowGender:''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('userInfo').avatarUrl == null || wx.getStorageSync('userInfo').nickName == null) {
      this.setData({
        wanShow: !this.data.wanShow
      })
    } else {
      console.log("通过历史数据获得用户信息");
      this.setData({
        userInfo: wx.getStorageSync('userInfo')
      })
    }
  },

  // input赋值
  setVal(e){
    console.log(e)
    let inforList = this.data.inforList;
    inforList.forEach((item,index)=>{
      if(e.currentTarget.dataset.index == index){
        item.val = e.detail.value;
        this.setData({inforList:inforList})
      }
    })

  },
  // 开始测试
  toCeshi(){
    let inforList = this.data.inforList;
    console.log(inforList)
    let flag = inforList.every(item=>{
      return item.val!=''
    })
    if(flag){
      if(this.data.nowGender==''){
        wx.showToast({title: '请选择您的性别',icon:'none'})
      }else{
        let inforArr = this.data.inforList.concat([{text:'请选择您的性别',val:this.data.nowGender}]);
        wx.navigateTo({
          url: '/pages/result/result?inforArr='+JSON.stringify(inforArr),
        })
      }
      
    }else{
      inforList.forEach(item=>{
        wx.showToast({title: item.text,icon:'none'})
      })
      
    }
  },
  // 选择性别
  radioChange(e) {
    let genderList = this.data.genderList; 
    genderList.forEach(item=>{
      item.checked = '';
      if(item.value == e.detail.value){
        item.checked = 'true';
      }
      
   });
   this.setData({genderList:genderList});
  },
  // 打开/关闭性别弹窗
  openGenderBox(){
    
    let genderList = this.data.genderList; 
    let gender = ''; 
    genderList.forEach(item=>{
       if(item.checked){
         gender = item.name
       }
    });
      
    this.setData({pupStatus:!this.data.pupStatus,nowGender:gender});
  }
})